package topic.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.cpoopc.scrollablelayoutlib.ScrollableHelper;
import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import topic.activity.TopicDetail;
import topic.adapter.TopicAdapter;
import topic.bean.TopicGroupBean;
import utils.MyLogUtils;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/3/20.
 * 用于热门讨论
 */
@SuppressLint("ValidFragment")
public class TopicFragment extends BaseFragment implements View.OnClickListener, ScrollableHelper.ScrollableContainer {
    private View view;
    private MyPtrRefresh_LoadView topic_fragment_myLoadLayout;
    private ListView topic_fragment_lv;
    private TopicAdapter topicAdapter;
    private ArrayList<TopicGroupBean.BlogListBean> arrayList;
    private int page;
    private String type;
    private static final String TAG = "TopicFragment";
    public TopicFragment(String type) {
        this.type = type;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    TopicGroupBean topicGroupBean = gson.fromJson((String) msg.obj, TopicGroupBean.class);
                    if (topicGroupBean != null) {
                        List<TopicGroupBean.BlogListBean> blogList = topicGroupBean.getBlogList();
                        if (blogList.size() > 0) {
                            if (page == 1) {
                                //重新加载
                                arrayList.clear();
                            }
                            for (int i = 0; i < blogList.size(); i++) {
                                TopicGroupBean.BlogListBean bean = blogList.get(i);
                                arrayList.add(bean);
                            }
                            topicAdapter.notifyDataSetChanged();
                        } else {
                            page--;
                        }
                        topic_fragment_myLoadLayout.OnRefreshComplete();
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            view = LayoutInflater.from(getMyActivity()).inflate(R.layout.topic_fragment, null);
        }
        initView();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        page = 1;
        getDataFromInterNet(page);
        return view;
    }

    private void initView() {
        arrayList = new ArrayList<>();
        topic_fragment_myLoadLayout = (MyPtrRefresh_LoadView) view.findViewById(R.id.topic_fragment_myLoadLayout);
        topic_fragment_myLoadLayout.ChangeMode(false, true);
        topic_fragment_myLoadLayout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page++;
                getDataFromInterNet(page);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        topic_fragment_lv = (ListView) view.findViewById(R.id.topic_fragment_lv);

        topicAdapter = new TopicAdapter(getMyActivity(), arrayList, new TopicAdapter.OnTopicDetailListener() {
            @Override
            public void onTopicDetailListener(int position) {
                Intent i = new Intent(getMyActivity(), TopicDetail.class);
                i.putExtra("blogid", arrayList.get(position).getBlogid());
                TopicFragment.this.startActivityForResult(i, position);
            }
        });
        topic_fragment_lv.setAdapter(topicAdapter);
    }

    @Override
    public void onClick(View v) {

    }

    private void getDataFromInterNet(int page) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", String.valueOf(page));
        hashMap.put("type", type);
        //TODO
        if (handler != null) {
            new InterNetController(getMyActivity(), Constant.SHOWBLOG, handler, hashMap, 0, false);
        }
    }

    @Override
    public View getScrollableView() {
        return topic_fragment_lv;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyLogUtils.e(TAG, "result" + resultCode);
        if (resultCode == 2) {
            int zan = data.getIntExtra("zan", 0);
            int comment = data.getIntExtra("comment", 0);
            boolean is_praised = data.getBooleanExtra("is_praised", false);
            boolean is_deleted = data.getBooleanExtra("is_deleted", false);
            if (is_deleted) {
                //删除博客
                arrayList.remove(requestCode);
                topicAdapter.notifyDataSetChanged();
                return;
            } else {
                arrayList.get(requestCode).setPrise(String.valueOf(zan));
                arrayList.get(requestCode).setComment(String.valueOf(comment));
                if (is_praised) {
                    //赞
                    arrayList.get(requestCode).setPrised(1);
                } else {
                    //取消赞
                    arrayList.get(requestCode).setPrised(0);
                }
                topicAdapter.notifyDataSetChanged();
            }
        }
    }
}
